<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class mysql_functions {

  public function create_dbconnect($database_host,$default_database,$database_login='',$database_pass='') {
    $this->dbconn=false;
    if (!$this->dbconn=mysql_connect($database_host, $database_login, $database_pass)) { echo "Cannot Open Connection ".mysql_error();$this->dbconn=false; }
    if (!mysql_select_db($default_database,$this->dbconn)) { echo "Cannot Select DB".mysql_error();$this->dbconn=false; }
   return;
  }

  public function fetch_rows($db,$where='',$order='',$limit='') {
    global $masterclass;

    $result=array();

    $strsql = sprintf("SELECT * FROM `%s`",$this->formatmysqlstr($db));
    $strsql .= $this->whereorderlimit($where,$order,$limit);
    $result = $this->fetch_assoc($strsql);

    return $result;

  }

  public function fetch_assoc($strsql) {
    global $masterclass;

    $result=array();

    $query = mysql_query($strsql,$this->dbconn) or die ("!!!002!!!".mysql_error());
    if (mysql_num_rows($query)!=0) {
      while ($resultline = mysql_fetch_assoc($query)) {
        $result[]=$resultline;
      }
      return $result;
    }
    mysql_free_result($query);

    return false;
  }

  public function fetch_query($strsql) {
    global $masterclass;

    $query = mysql_query($strsql,$this->dbconn) or die ("!!!002!!!".mysql_error());

    return $query;
  }

  public function fetch_tablecolumns($table) {
    $fieldtypes=array();
    $strsql = sprintf("SHOW COLUMNS FROM `%s`",$this->formatmysqlstr($table));
    $tablefields = $this->fetch_assoc($strsql);
    foreach ($tablefields as $fieldinfo) {
      $fieldtypes[]=$fieldinfo['Field'];
    }
  return $fieldtypes;
  }
  public function fetch_fieldtype($table,$column) {
    if (!isset($this->fieldtypes)) { $this->fieldtypes=array(); }
    if (!isset($this->fieldtypes[$table])) {
      $this->fieldtypes[$table]=$this->grab_tablecolumns($table);
    }
    return $this->fieldtypes[$table][$column]['Type'];
  }

  public function run_query($query) {
    global $masterclass;

    $strsql = sprintf("%s",$query);
    if (mysql_query($strsql,$this->dbconn) or die ('!!!!'.mysql_error())) { return true; }
    return false;
  }
  public function run_insert($query) {
    global $masterclass;

    $strsql = sprintf("%s",$query);
    if (mysql_query($strsql,$this->dbconn)) { return mysql_insert_id($this->dbconn); }
    return false;
  }
  public function formatmysqlstr($str) {
    $tmpstr=$str;
    $tmpstr=stripslashes($tmpstr);
    $tmpstr=mysql_real_escape_string($tmpstr);
    return $tmpstr;
  }
  public function formatmysqldbl($str) {
    $tmpstr='';
    $tmpstr=preg_replace("/[^0-9\.]/",'',$str);

    return $tmpstr;
  }

  private function whereorderlimit($where='',$order='',$limit='') {

    @$alimit=explode(',',$limit);
    if (is_array($alimit)) { $limitc=$alimit[0];$limits=$alimit[1]; } else
    if ($limit!='') { $limitc=$limit;$limits=0; }

    if ($where!='') { $strsql .= sprintf(" WHERE %s",$where); }
    if ($order!='') { $strsql .= sprintf(" ORDER BY %s",$order); }
    if ($limitc!='') { $strsql .= sprintf(" LIMIT %d,%d",$limitc,$limits); }
    return $strsql;
  }

}
?>